package com.example.bookmanager_back.utils;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

public class RedisUtils {
    private final JedisPool pool;

    public RedisUtils(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(128); // 设置最大连接数
        poolConfig.setMaxIdle(4);   // 设置最大空闲连接数
        poolConfig.setMinIdle(1);    // 设置最小空闲连接数
        poolConfig.setMaxWait(Duration.ofSeconds(1)); // 设置连接池的最大阻塞等待时间

        pool = new JedisPool(poolConfig, "localhost", 6379);
    }

    public void setJWT_BLACK_LIST(String uuid, long expire){
        try(Jedis jedis = pool.getResource()){
            jedis.setex(Const.JWT_BLACK_LIST + uuid, expire / 1000, "");
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public boolean isInJWT_BLACK_LIST(String uuid){
        try(Jedis jedis = pool.getResource()){
            return jedis.exists(Const.JWT_BLACK_LIST + uuid);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}